import { configureStore } from '@reduxjs/toolkit';
import rootReducer from './rootReducer';
import { createEpicMiddleware } from 'redux-observable';
import rootEpic from './rootEpic';
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';

const epicMiddleware = createEpicMiddleware();

/**
 * 构建 store
 */
const store = configureStore({
  reducer: rootReducer,
  middleware: getDefaultMiddleware => getDefaultMiddleware().concat(epicMiddleware),
});

epicMiddleware.run(rootEpic);
export type TRootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch
export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<TRootState> = useSelector;
export default store;